/**
 * 
 */
package myfamily.model;

import java.util.Collection;
import java.util.Date;

import javax.jdo.Query;

import com.versant.odmg.Database;

/**
 * @author Vineet
 *
 */
public class RegisteredUser extends GenericPerson
{
	private String userName;
	private String password;
	private String openIDURI;

	public RegisteredUser() {
		super();
	}


	public RegisteredUser(String openIDString, String uName, String pwd,Name pName, String gender, String religion, String ethnicity, Date dob, Date dop, Contact contactNo) throws Exception
	{
		super(openIDString,pName, gender, religion, ethnicity, dob, dop,contactNo);

		//Checking if the username is already database
		DatabaseObject db = DatabaseObjectFactory.getDatabaseObject();
		Query q = db.pm.newQuery(RegisteredUser.class);
		
		q.setFilter("userName == \"" + uName + "\"");
		
		Collection ans = null;
		try{
			ans = (Collection)q.execute();
			if(!ans.isEmpty())
				throw(new Exception("The User Name" + uName + "is already in use"));
		}
		/*catch(Exception e)
		{
			System.out.println(e.getMessage());
		}*/
		finally
		{
			q.close(ans);
		}
		
		db.pm.currentTransaction().begin();
		userName = uName;
		password = pwd;
		openIDURI = openIDString;
		db.pm.currentTransaction().commit();
		this.setPassword(pwd);
		this.setAddedBy(this);
	}


	public void persist()
	{
		DatabaseObject db = DatabaseObjectFactory.getDatabaseObject();
		db.pm.currentTransaction().begin();
		db.pm.makePersistent(this);
		db.pm.currentTransaction().commit();
	}

	public GenericPerson createUnregisteredUser(Name pName, String gender, String religion, String ethnicity, Date dob, Date dop, Contact contactNo)
	{
		GenericPerson ret = new GenericPerson(openIDURI,pName, gender, religion, ethnicity, dob, dop, contactNo);
		ret.setAddedBy(this);
		return ret;
	}

	public GenericPerson createDummyProfile()
	{
		GenericPerson ret = new GenericPerson(openIDURI,new Name("Empty Profile", "",""), "", "", "", null, null, null);
		ret.setAddedBy(this);
		return ret;
	}


	public boolean loginSuccessful(String uName, String pwd)
	{
		if(this.userName.equals(uName) && this.password.equals(pwd))
			return true;
		else
			return false;
	}


	public String getUserName() {
		return userName;
	}
	/**
	 * @param userName the userName to set
	 */
/*	public void setUserName(String userName,DatabaseObject db) {
		db.pm.currentTransaction().begin();
		this.userName = userName;
		db.pm.currentTransaction().commit();
	}
	*/
	
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password)
	{
		DatabaseObject db = DatabaseObjectFactory.getDatabaseObject();
		db.pm.currentTransaction().begin();
		this.password = password;
		db.pm.currentTransaction().commit();
	}


	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		if(this.getFather() != null)
			return super.toString() + " Child of " + this.getFather().getPersonName().toString();
		else
			return super.toString();
	}

	public static RegisteredUser login(String OpenIdString)
    {
            DatabaseObject db = DatabaseObjectFactory.getDatabaseObject();
            Query q = db.pm.newQuery(RegisteredUser.class);
            RegisteredUser ret = null;
            
            q.setFilter("openIDURI == \"" + OpenIdString + "\"");
            //q.setFilter("userName == \"Vin1\"");
            Collection ans = null;
            try{
                    ans = (Collection)q.execute();
                    if(!ans.isEmpty())
                            ret = (RegisteredUser)ans.toArray()[0];
            }
            catch (Exception e) {
                    ret = null;
            }
            return ret;
    }

}